package com.gitee.wsl.android.ext.service.alter

import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import com.gitee.wsl.service.AbstractAlarmServer
import com.gitee.wsl.service.AbstractAlarmTask
import com.gitee.wsl.service.Alarm
import com.gitee.wsl.service.AlarmItem
import com.gitee.wsl.service.RECEIVER_ALARM_ID
import com.gitee.wsl.service.RECEIVER_ALARM_MESSAGE
import com.gitee.wsl.time.toEpochMilliseconds
import kotlinx.datetime.Instant


class SampleAlterServer(private val context: Context): AbstractAlarmServer() {

    private val alarmManager = context.getSystemService(AlarmManager::class.java)

    override fun schedule(item: AlarmItem) {
        val intent = Intent(context, AlarmReceiver::class.java).apply {
            putExtra(RECEIVER_ALARM_ID,item.id)
            putExtra(RECEIVER_ALARM_MESSAGE, item.message)
        }

        alarmManager.setExactAndAllowWhileIdle(
            AlarmManager.RTC_WAKEUP,
            item.time.toEpochMilliseconds(),
            PendingIntent.getBroadcast(
                context,
                item.hashCode(),
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
            )
        )
    }

    override fun cancel(item: AlarmItem) {
        alarmManager.cancel(
            PendingIntent.getBroadcast(
                context,
                item.hashCode(),
                Intent(context, AlarmReceiver::class.java),
                PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
            )
        )
    }

}

class SampleAlarmTask(val item: AlarmItem): AbstractAlarmTask() {
    override fun start() {
        TODO("Not yet implemented")
    }

    override fun stop() {
        if(isRunning())
            Alarm.alarmServer.cancel(item)
    }

    override fun pause() {
        TODO("Not yet implemented")
    }

    override fun cancel() {
        TODO("Not yet implemented")
    }

    override fun fireAlarmReceive() {
        TODO("Not yet implemented")
    }

    override fun retry(): Int {
        TODO("Not yet implemented")
    }

     override fun snooze(data: Instant) {
        TODO("Not yet implemented")
    }

    override fun active() {
        TODO("Not yet implemented")
    }

    override fun deactivate() {
        TODO("Not yet implemented")
    }
}